﻿using CommonEngine.Power;
using System;
using System.Collections.Generic;
using System.Linq;

namespace CommonDevelop.Scene
{
    /// <summary>
    /// 当前上下文执行用户信息
    /// </summary>
    [Serializable]
    public class UserInfo
    {
        #region 用户信息

        /// <summary>
        /// 用户主键
        /// </summary>		
        public string UserId { get; set; }
        /// <summary>
        /// 应用域Key
        /// </summary>	
        public string ApplyKey { get; set; }
        /// <summary>
        /// 账户
        /// </summary>	
        public string Account { get; set; }
        /// <summary>
        /// 密码秘钥
        /// </summary>	
        public string Secretkey { get; set; }
        /// <summary>
        /// 安全级别
        /// </summary>	
        public string SecurityLevel { get; set; }
        /// <summary>
        /// 是否是超级管理员
        /// </summary>
        public bool IsAdmin { get; set; }

        /// <summary>
        /// 真实姓名
        /// </summary>
        public string RealName { get; set; }
        /// <summary>
        /// 呢称
        /// </summary>	
        public string NickName { get; set; }
        /// <summary>
        /// 头像
        /// </summary>	
        public string HeadIcon { get; set; }

        /// <summary>
        /// 工号
        /// </summary>
        public string JobNumber { get; set; }
        /// <summary>
        /// 用户全称
        /// </summary>
        public string FullName { get; set; }

        /// <summary>
        /// 公司主键
        /// </summary>		
        public string companyId { get; set; }
        /// <summary>
        /// 部门主键
        /// </summary>		
        public string departmentId { get; set; }

        #endregion

        /// <summary>
        /// 票据信息
        /// </summary>
        public string Token { get; set; }
        /// <summary>
        /// API票据信息
        /// </summary>
        public string JWT_Token { get; set; }
        /// <summary>
        /// 商务编号
        /// </summary>
        public string BusinessID { get; set; }
        /// <summary>
        /// IP地址
        /// </summary>
        public string IP { get; set; }
        /// <summary>
        /// 域地址
        /// </summary>
        public string DomainPath { get; set; }

        #region 扩展信息

        /// <summary>
        /// 用户权限资源配置数据
        /// </summary>
        public List<ClientPower> Power { get; set; }
        /// <summary>
        /// 用户配置菜单，加入用户缓存，进session
        /// </summary>
        public List<SysMenu> MyMenu { get; set; }

        /// <summary>
        /// 用户资源配置权值字典，充血方法
        /// </summary>
        public Dictionary<Guid, int> UserPermissions()
        {
            if (this.Power.Any())
            {
                Dictionary<Guid, int> dic = new Dictionary<Guid, int>();
                this.Power.ForEach(a => dic.Add(new Guid(a.ResourceId), a.Grant));
                return dic;
            }
            else
                return null;
        }

        /// <summary>
        /// 追加系统菜单并输出
        /// </summary>
        public List<SysMenu> GetMenu(List<SysMenu> MenuLs)
        {
            List<SysMenu> tableData = this.MyMenu;
            if (tableData == null)
                tableData = new List<SysMenu>();

            if (this.IsAdmin)
                tableData.AddRange(MenuLs);

            return tableData;
        }

        /// <summary>
        /// userId转为GUID输出
        /// </summary>
        public Guid GetUserGUID()
        {
            Guid gid = Guid.Empty;
            if (string.IsNullOrWhiteSpace(this.UserId))
                return gid;

            Guid.TryParse(this.UserId, out gid);
            return gid;
        }

        #endregion
    }
}
